package com.sky.controller.admin;

import com.sky.exception.BaseException;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@Api(tags = "文件上传")
@RequestMapping("/admin/common")
public class UploadController {

    @Autowired
    AliOssUtil aliOssUtil;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result uploadImg(MultipartFile file)  {
        log.info("图片上传："+file);
        String originalFilename = file.getOriginalFilename();
        String fileName= UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
        log.info(fileName);
        String imgUrl=null;
        try {
            imgUrl  = aliOssUtil.upload(file.getBytes(),fileName);
        } catch (IOException e) {
            //throw new BaseException("菜品图片上传失败！");
            Result.error("菜品图片上传失败！");
        }
        return Result.success(imgUrl);
    }
}
